package algorithm;

import java.util.*;

public class TreeNodeTest {
    public static void main(String[] args) {
        TreeNode root = TreeUtil.build();
        //preOrder(root);//1245367
        System.out.println("------------------");
        //midOrder(root);// 4251637
        System.out.println("------------------");
        //postOrder(root);// 4526731
        System.out.println("------------------");
        //postOrderStack(root);
        inOrder2(root);
    }

    // 深度优先遍历
    // 前序遍历
    public static void preOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.data);
        preOrder(node.left);
        preOrder(node.right);
    }
    // 中序遍历
    public static void inOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.data);
        inOrder(node.right);
    }
    // 后序遍历
    public static void postOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.data);
    }

    // 深度优先遍历非递归
    // 迭代法实现前序遍历
    public static void preOrder2(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            System.out.println(node.data);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }
    // 迭代法后序遍历
    public static void postOrder2(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> res = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            res.push(node);
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        while (!res.isEmpty()) {
            System.out.println(res.pop().data);
        }
    }

    // 迭代法中序遍历
    private static void inOrder2(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                TreeNode node = stack.pop();
                System.out.print(node.data);
                cur = node.right;
            }
        }
    }

    // 广度优先遍历
    public static void widthOrder(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.remove();
            System.out.println(node.data);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }
}
